package cn.h8k.hj212.codec.impl;

import cn.h8k.hj212.codec.Hj212Encoder;
import cn.h8k.hj212.constant.Hj212Constants;
import java.util.Map;

/**
 * HJ212协议编码器默认实现
 * 可被继承以自定义编码行为
 */
public class DefaultHj212Encoder implements Hj212Encoder {
    
    /**
     * 将数据字典转换为HJ212协议字符串
     * @param dic 包含协议字段的Map
     * @return 符合HJ212-2017标准的协议字符串
     */
    @Override
    public String encode(Map<String, Object> dic) {
        StringBuilder data = new StringBuilder();
        
        // 构建数据部分
        for (Map.Entry<String, Object> entry : dic.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            
            if (Hj212Constants.CP.equals(key)) {
                data.append(Hj212Constants.CP).append(Hj212Constants.KEY_VALUE_SEPARATOR)
                    .append(Hj212Constants.GROUP_SEPARATOR);
                @SuppressWarnings("unchecked")
                Map<Integer, Map<String, Object>> cpMap = (Map<Integer, Map<String, Object>>) value;
                boolean firstCp = true;
                
                for (Map.Entry<Integer, Map<String, Object>> cpEntry : cpMap.entrySet()) {
                    if (!firstCp) {
                        data.append(Hj212Constants.FIELD_SEPARATOR);
                    }
                    
                    Map<String, Object> cpValues = cpEntry.getValue();
                    boolean firstItem = true;
                    
                    for (Map.Entry<String, Object> cpItem : cpValues.entrySet()) {
                        if (!firstItem) {
                            data.append(Hj212Constants.ITEM_SEPARATOR);
                        }
                        data.append(cpItem.getKey()).append(Hj212Constants.KEY_VALUE_SEPARATOR)
                            .append(cpItem.getValue());
                        firstItem = false;
                    }
                    firstCp = false;
                }
                data.append(Hj212Constants.GROUP_SEPARATOR);
            } else {
                data.append(key).append(Hj212Constants.KEY_VALUE_SEPARATOR)
                    .append(value).append(Hj212Constants.FIELD_SEPARATOR);
            }
        }
        
        return data.toString();
    }
    
    /**
     * 保护方法：子类可重写以自定义CP字段的编码
     * @param cpMap CP数据映射
     * @return 编码后的CP字符串
     */
    protected String encodeCpField(Map<Integer, Map<String, Object>> cpMap) {
        StringBuilder cpData = new StringBuilder();
        boolean firstCp = true;
        
        for (Map.Entry<Integer, Map<String, Object>> cpEntry : cpMap.entrySet()) {
            if (!firstCp) {
                cpData.append(Hj212Constants.FIELD_SEPARATOR);
            }
            
            Map<String, Object> cpValues = cpEntry.getValue();
            boolean firstItem = true;
            
            for (Map.Entry<String, Object> cpItem : cpValues.entrySet()) {
                if (!firstItem) {
                    cpData.append(Hj212Constants.ITEM_SEPARATOR);
                }
                cpData.append(cpItem.getKey()).append(Hj212Constants.KEY_VALUE_SEPARATOR)
                    .append(cpItem.getValue());
                firstItem = false;
            }
            firstCp = false;
        }
        
        return cpData.toString();
    }
} 